package cn.nawang.ebeim.api.repository.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;

import org.hibernate.SQLQuery;
import org.hibernate.transform.Transformers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import com.alibaba.fastjson.JSONObject;

import cn.nawang.ebeim.api.bean.ClassBean;
import cn.nawang.ebeim.api.bean.DatasourceMenuBean;
import cn.nawang.ebeim.api.comm.BaseQuery;
import cn.nawang.ebeim.api.comm.PageList;
import cn.nawang.ebeim.api.comm.Pages;
import cn.nawang.ebeim.api.domain.Datasource;
import cn.nawang.ebeim.api.utils.ConvertUtil;
import cn.nawang.ebeim.api.utils.RefectSql;
import cn.nawang.ebeim.api.vo.DatasourceView;

/**
 * @ClassName: DatasourceQueryRepositoryImpl
 * @Description: 数据源dao层
 * @author 张建峰
 * @date 2015年8月10日10:25:49
 * @version v1.0
 */
@Repository
public class DatasourceQueryRepositoryImpl extends BaseQuery {

  @Autowired
  private EntityManagerFactory entityManagerFactory;

  /**
   * @author 张建峰
   * @Description: 计算记录总数
   * @param @return
   * @date 2015年8月10日10:27:45
   *
   * @修改人 张建峰
   * @修改日期
   */
  public int countAllData() {
    int count = 0;
    String sql = "select count(*) from tb_datesource";
    EntityManager em = entityManagerFactory.createEntityManager();
    try {
      Query query = em.createNativeQuery(sql);
      List objList = query.getResultList();
      if (objList != null && objList.size() > 0) {
        count = Integer.valueOf(objList.get(0).toString());
      }
    } catch (Exception ex) {
      ex.printStackTrace();
      ex.printStackTrace();
    } finally {
      em.close();
    }
    return count;
  }

  /**
   * @author 张建峰
   * @Description: 根据sql查询count
   * @param @param sql
   * @param @return
   * @return int
   * @date 2015年8月10日10:27:39
   *
   * @修改人 张建峰
   * @修改日期
   */
  public int countData(String sql, List<Object> paramsList) {
    EntityManager em = entityManagerFactory.createEntityManager();
    int count = 0;
    try {
      Query query = em.createQuery(sql);
      for (int i = 0; i < paramsList.size(); i++) {
        query.setParameter(i + 1, paramsList.get(i));
      }
      count = query.getResultList().size();
      return count;
    } catch (Exception ex) {
      ex.printStackTrace();
    } finally {
      em.close();
    }
    return count;
  }

  /**
   * @author 张建峰
   * @Description: 根据条件分页查询
   * @param @param t
   * @param @param page
   * @param @return
   * @return PageList
   * @date 2015年8月10日10:27:34
   *
   * @修改人 张建峰
   * @修改日期
   */
  public PageList findByDatasource(Datasource t, Pages page) {
    PageList pl = null;
    String sql = "SELECT new cn.nawang.ebeim.api.bean.DatasourceBean"
        + "(c.isEncrypt,d.dsSecurityRank,d.id,d.customerId,d.terminalId,d.name,"
        + "d.createUserId,d.createTime,d.description,"
        + "d.isOpenLocalBackup,d.dsType,d.status,d.lmUserId,d.lmTime,"
        + "d.spaceUsage,d.nextRun,d.count,d.currentVersionMajor,"
        + "d.currentVersionMinor,b.id,b.type,b.backupTime,"
        + "b.backupTimeStatus,b.backupCycle,b.backupPeriod,"
        + "d.configId,c.scanType,c.root,c.scanHideFile,"
        + "c.verifyCompleteName,c.verifyCompleteMaxCount,c.verifyCompleteTimer,"
        + "c.fileAttrGainer,c.recordCanNotReadFileInfo,c.recordEmptyDirectory,"
        + "c.recordEmptyFile,c.filterFileSuffix,c.dbUsername,c.dbPassword,"
        + "c.dbType,c.dbHost,c.dbPort,c.dbName,u.name,d.bindDeviceName,d.dsStatus)"
        + " from BackStrategy b,User u,Datasource d,DatasourceConfig c"
        + " where d.id = b.datasourceId" + " and d.createUserId = u.id"
        + " and d.configId = c.configId and 1=1 ";
    List<ClassBean> conditions = RefectSql.getClassBeans(t);
    List<Object> paramsList = new ArrayList<Object>();
    for (int i = 0; i < conditions.size(); i++) {
      ClassBean classBean = conditions.get(i);
      if (classBean.getContent() != null) {
        sql += " and d." + classBean.getName() + "= ?";
        paramsList.add(classBean.getContent());
      }
    }

    int count = this.countData(sql, paramsList);
    page.setTotalRecord(count);
    page.setTotalPage((int) Math.ceil((count * 1.0) / page.getPageSize()));
    EntityManager em = entityManagerFactory.createEntityManager();
    try {
      Query query = em.createQuery(sql);
      query.setFirstResult(page.getStartPos());
      query.setMaxResults(page.getPageSize());
      for (int i = 0; i < paramsList.size(); i++) {
        query.setParameter(i + 1, paramsList.get(i));
      }
      List objList = query.getResultList();
      pl = new PageList();
      pl.setObjList(objList);
      pl.setPage(page);
      pl.setSuccess(true);
    } catch (Exception ex) {
      ex.printStackTrace();
    } finally {
      em.close();
    }
    return pl;
  }

  public PageList findAllByPaging(Pages page) {
    String hqlList = "SELECT new cn.nawang.ebeim.api.bean.DatasourceBean"
        + "(c.isEncrypt,d.dsSecurityRank,d.id,d.customerId,d.terminalId,d.name,"
        + "d.createUserId,d.createTime,d.description,"
        + "d.isOpenLocalBackup,d.dsType,d.status,d.lmUserId,d.lmTime,"
        + "d.spaceUsage,d.nextRun,d.count,d.currentVersionMajor,"
        + "d.currentVersionMinor,b.id,b.type,b.backupTime,"
        + "b.backupTimeStatus,b.backupCycle,b.backupPeriod,"
        + "d.configId,c.scanType,c.root,c.scanHideFile,"
        + "c.verifyCompleteName,c.verifyCompleteMaxCount,c.verifyCompleteTimer,"
        + "c.fileAttrGainer,c.recordCanNotReadFileInfo,c.recordEmptyDirectory,"
        + "c.recordEmptyFile,c.filterFileSuffix,c.dbUsername,c.dbPassword,"
        + "c.dbType,c.dbHost,c.dbPort,c.dbName,u.name,d.bindDeviceName,d.dsStatus)"
        + " from BackStrategy b,User u,Datasource d,DatasourceConfig c"
        + " where d.id = b.datasourceId and d.createUserId = u.id"
        + " and d.configId = c.configId and d.dsType<>'3' and 1=1 ";
    String hqlCount =
        "select count(d.id) from BackStrategy b," + "User u,Datasource d,DatasourceConfig c"
            + " where d.id = b.datasourceId and d.createUserId = u.id"
            + " and d.configId = c.configId and d.dsType<>'3' and 1=1 ";
    String order = " order by d.id desc";
    PageList pl = null;
    try {
      String cod = joinSearchCondition(page);// 拼接查询条件
      pl = queryListByPaging(hqlCount + cod + order, hqlList + cod + order, page);
    } catch (Exception ex) {
      ex.printStackTrace();
    }
    return pl;
  }

  /**
   * @author 张建峰
   * @Description: 根据某个属性查询列表
   * @param @param property
   * @param @param value
   * @param @return
   * @return List<Datasource>
   * @date 2015年8月10日10:27:27
   *
   * @修改人 张建峰
   * @修改日期
   */
  public List<Datasource> findByParam(String property, Object value) {
    List objList = null;
    String sql = "from Datasource obj where obj." + property + " = ?1";
    EntityManager em = entityManagerFactory.createEntityManager();
    try {
      Query query = em.createQuery(sql);
      query.setParameter(1, value);
      objList = query.getResultList();
    } catch (Exception ex) {
      ex.printStackTrace();
    } finally {
      em.close();
    }
    return objList;
  }

  // /**
  // * @author 张建峰
  // * @Description: 根据终端id和客户Id分页查询列表
  // * @param @param terminalId
  // * @param @return
  // * @return List<Datasource>
  // * @date 2015年8月10日10:27:10
  // *
  // * @修改人 张建峰
  // * @修改日期
  // */
  // public PageList findByCstIdAndTerId(String customerId,String terminalId,Pages page) {
  // PageList pl = null;
  // List<Object> list = null;
  // String sql = "SELECT new cn.nawang.ebeim.api.bean.DatasourceBean"
  // +"(d.dsSecurityRank,d.id,d.customerId,d.terminalId,d.name,"
  // + "d.createUserId,d.createTime,d.description,"
  // + "d.isOpenLocalBackup,d.dsType,d.status,d.lmUserId,d.lmTime,"
  // + "d.spaceUsage,d.nextRun,d.count,d.currentVersionMajor,"
  // + "d.currentVersionMinor,b.id,b.type,b.backupTime,"
  // + "b.backupTimeStatus,b.backupCycle,b.backupPeriod,"
  // + "d.configId,c.scanType,c.root,c.scanHideFile,"
  // + "c.verifyCompleteName,c.verifyCompleteMaxCount,c.verifyCompleteTimer,"
  // + "c.fileAttrGainer,c.recordCanNotReadFileInfo,c.recordEmptyDirectory,"
  // + "c.recordEmptyFile,c.filterFileSuffix,c.dbUsername,c.dbPassword,"
  // + "c.dbType,c.dbHost,c.dbPort,c.dbName,u.name)"
  // +" from BackStrategy b,User u,Datasource d,DatasourceConfig c"
  // +" where d.id = b.datasourceId"
  // +" and d.createUserId = u.id"
  // +" and d.configId = c.configId"
  // +" and d.customerId = ?"
  // +" and d.terminalId = ?";
  // List<Object> paramsList = new ArrayList<Object>();
  // paramsList.add(customerId);
  // paramsList.add(terminalId);
  //
  // int count = this.countData(sql,paramsList);
  // page.setTotalRecord(count);
  // page.setTotalPage((int)Math.ceil((count*1.0)/page.getPageSize()));
  // try{
  // EntityManager em = entityManagerFactory.createEntityManager();
  // Query query = em.createQuery(sql);
  // query.setFirstResult(page.getStartPos());
  // query.setMaxResults(page.getPageSize());
  // query.setParameter(1, customerId);
  // query.setParameter(2, terminalId);
  // list = query.getResultList();
  // em.close();
  // pl = new PageList();
  // pl.setObjList(list);
  // pl.setPage(page);
  // pl.setSuccess(true);
  // }catch(Exception ex){
  // ex.printStackTrace();
  // }
  // return pl;
  // }

  /**
   * 
   * @author fanyb
   * @Description: 根据属性名和属性值查找datasourceView
   * @param property 属性名
   * @param value 属性值
   * @return List<DatasourceView>
   * @date Sep 9, 20153:51:03 PM
   *
   * @修改人
   * @修改日期
   */
  public List<DatasourceView> findDatasourceViewByParam(String property, Object value) {
    List<DatasourceView> objList = null;
    String sql =
        "select new cn.nawang.ebeim.api.vo.DatasourceView(dc.isEncrypt,ds.dsSecurityRank,ds.id,ds.customerId,ds.terminalId,ds.name,ds.createUserId,"
            + "ds.createTime,ds.description,ds.isOpenLocalBackup,ds.dsType,ds.status,ds.configId,ds.lmUserId,ds.lmTime,ds.spaceUsage,"
            + "ds.nextRun,ds.count,ds.currentVersionMajor,ds.currentVersionMinor,dc.scanType,dc.root,dc.scanHideFile,"
            + "dc.verifyCompleteName,dc.verifyCompleteMaxCount,dc.verifyCompleteTimer,dc.fileAttrGainer,dc.recordCanNotReadFileInfo,"
            + "dc.recordEmptyDirectory,dc.recordEmptyFile,dc.filterFileSuffix,dc.dbUsername,dc.dbPassword,dc.dbType,"
            + "dc.dbHost,dc.dbPort,dc.dbName,bs.id,bs.type,bs.backupTime,bs.backupTimeStatus,"
            + "bs.backupCycle,bs.backupPeriod,ds.bindDeviceName,ds.dsStatus) from BackStrategy bs,Datasource ds,DatasourceConfig dc "
            + "where ds.configId=dc.configId and bs.datasourceId=ds.id and ds." + property
            + " =:value";
    try {
      Map<String, Object> paraMap = new HashMap<String, Object>();
      paraMap.put("value", value);
      objList = queryObject(sql, paraMap);
    } catch (Exception ex) {
      ex.printStackTrace();
    }
    return objList;
  }

  /**
   * @author zhangjf
   * @Description: 分页查询
   * @param @param property
   * @param @param value
   * @param @param currPage
   * @param @param pageSize
   * @param @return
   * @return JSONObject
   * @date 2015年10月12日下午3:07:35
   *
   * @修改人 zhangjf
   * @修改日期
   */
  public JSONObject findDatasourceViewByPage(String property, Object value, Integer currPage,
      Integer pageSize) {
    List<DatasourceView> objList = null;
    String sql =
        "select new cn.nawang.ebeim.api.vo.DatasourceView(dc.isEncrypt,ds.dsSecurityRank,ds.id,ds.customerId,ds.terminalId,ds.name,ds.createUserId,"
            + "ds.createTime,ds.description,ds.isOpenLocalBackup,ds.dsType,ds.status,ds.configId,ds.lmUserId,ds.lmTime,ds.spaceUsage,"
            + "ds.nextRun,ds.count,ds.currentVersionMajor,ds.currentVersionMinor,dc.scanType,dc.root,dc.scanHideFile,"
            + "dc.verifyCompleteName,dc.verifyCompleteMaxCount,dc.verifyCompleteTimer,dc.fileAttrGainer,dc.recordCanNotReadFileInfo,"
            + "dc.recordEmptyDirectory,dc.recordEmptyFile,dc.filterFileSuffix,dc.dbUsername,dc.dbPassword,dc.dbType,"
            + "dc.dbHost,dc.dbPort,dc.dbName,bs.id,bs.type,bs.backupTime,bs.backupTimeStatus,"
            + "bs.backupCycle,bs.backupPeriod,ds.bindDeviceName,ds.dsStatus) from BackStrategy bs,Datasource ds,DatasourceConfig dc "
            + "where ds.configId=dc.configId and bs.datasourceId=ds.id and ds." + property + " =?1";

    List<Object> paramsList = new ArrayList<Object>();
    paramsList.add(value);

    int count = this.countData(sql, paramsList);
    EntityManager em = entityManagerFactory.createEntityManager();
    try {
      Query query = em.createQuery(sql);
      query.setFirstResult(currPage);
      query.setMaxResults(pageSize);
      query.setParameter(1, value);
      objList = query.getResultList();
    } catch (Exception ex) {
      ex.printStackTrace();
    } finally {
      em.close();
    }
    JSONObject json = new JSONObject();
    json.put("count", count);
    json.put("list", objList);
    return json;
  }

  /**
   * 
   * @author: huanghj
   * @Description: TODO
   * @param @param 根据用户获得总备份数
   * @param @return
   * @return int
   * @date 2015年10月24日上午11:47:51
   *
   * @修改人
   * @修改日期
   */
  public int getBackupCountByUser(String userId) {
    String sql =
        "select sum(count) as backup_sum from tb_datasource WHERE create_user_id='" + userId + "'";
    List<Map> obj = find(sql);
    if (obj != null && obj.size() > 0 && obj.get(0).get("backup_sum") != null)
      return Integer.valueOf(obj.get(0).get("backup_sum").toString());
    return 0;
  }

  public List find(String sql) {
    List<Map> objList = null;
    EntityManager em = entityManagerFactory.createEntityManager();
    try {
      Query query = em.createNativeQuery(sql);
      query.unwrap(SQLQuery.class).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
      objList = query.getResultList();
    } catch (Exception ex) {
      ex.printStackTrace();
    } finally {
      em.close();
    }
    return objList;
  }

  /**
   * 
   * @author fanyb
   * @Description: 查找同一个设备下的root是否已存在
   * @param terminalId
   * @param root
   * @return Datasource
   * @date Oct 30, 20154:30:08 PM
   *
   * @修改人
   * @修改日期
   */
  public Datasource findByDeviceAndRoot(String terminalId, String root ,String createUserId) {
    String hql =
        "select ds from Datasource ds,DatasourceConfig dsc where ds.configId=dsc.configId and "
            + " ds.terminalId=:terminalId and dsc.root=:root and ds.dsType <> '1' "
            + "and ds.createUserId=:createUserId";
    Map<String, Object> paraMap = new HashMap<String, Object>();
    try {
      paraMap.put("terminalId", terminalId);
      paraMap.put("root", root);
      paraMap.put("createUserId", createUserId);
      List<Datasource> datasources = queryObject(hql, paraMap);
      if (datasources != null && !datasources.isEmpty()) {
        return datasources.get(0);
      }
    } catch (Exception ex) {
      ex.printStackTrace();
    }
    return null;
  }

  /**
   * 
   * @author: huanghj
   * @Description: 根据组合条件获取数据源总和
   * @param @param userId
   * @param @param terminalId
   * @param @param status
   * @param @return
   * @return int
   * @date 2015年10月29日下午5:18:11
   *
   * @修改人
   * @修改日期
   */
  public int countByUserId(String userId, String terminalId, String dsStatus, String dsType) {
    StringBuffer sb = new StringBuffer();
    sb.append("select count(1) as ds_sum from tb_datasource where create_user_id='" + userId + "'");
    if (!StringUtils.isEmpty(terminalId)) {
      sb.append(" and  terminal_id ='" + terminalId + "'");
    }
    if (!StringUtils.isEmpty(dsStatus)) {
      sb.append(" and ds_status=").append(dsStatus);
    }
    if (!StringUtils.isEmpty(dsType)) {
      sb.append(" and ds_type=").append(dsType);
    }
    List<Map> list = find(sb.toString());
    if (list != null && list.size() > 0) {
      return Integer.parseInt(list.get(0).get("ds_sum").toString());
    } else {
      return 0;
    }
  }
  
  /**
   * 
   * @author fanyb
   * @Description: 查找当前用户的设备名称
   * @param createUserId
   * @return List<Datasource> 
   * @date Nov 11, 201511:08:47 AM
   *
   * @修改人 
   * @修改日期
   */
  public List<DatasourceMenuBean> findBindDeviceNameByTerminalId(String createUserId) {
    StringBuffer sb = new StringBuffer();
    sb.append("select new cn.nawang.ebeim.api.bean.DatasourceMenuBean(id,terminalId,bindDeviceName,'0')"
        + " from Datasource where createUserId=:createUserId ");
    sb.append(" GROUP BY terminalId");
    try{
      Map<String,Object> paraMap = new HashMap<String,Object>();
      paraMap.put("createUserId", createUserId);
      List<DatasourceMenuBean> datasources = queryObject(sb.toString(),paraMap);
      return datasources;
    }catch(Exception ex){
      ex.printStackTrace();
    }
    return null;
  }

  /**
   * 
   * @author: huanghj
   * @Description: TODO
   * @param @param mac
   * @param @param 查看数据源集合
   * @param @return
   * @return List
   * @date 2015年11月10日下午3:52:53
   *
   * @修改人
   * @修改日期
   */
  public List findListByUserId(String mac, String userId) {
    List objList = null;
    StringBuffer sb=new StringBuffer();
    sb.append(" select a.id,name,a.terminal_id,bind_device_name,next_run,ds_security_rank,a.status,ds_status,space_usage,ds_type,MAX(begin_time) as begin_time ");
    sb.append(" from tb_datasource a   LEFT JOIN tb_backup_log b ON  a.id=b.ds_id");
    sb.append("  where  create_user_id="+userId+" and ds_type=0 ");
//    sb.append(" select a.id,name,a.terminal_id,bind_device_name,next_run,ds_security_rank,ds_status,space_usage,ds_type,MAX(begin_time) as begin_time ");
//    sb.append(" from tb_datasource a   LEFT JOIN tb_backup_log b ON  a.id=b.ds_id");
//    sb.append("  where  create_user_id="+userId+" and ds_type!=2 ");
    
    if (mac != null && mac.length() != 0) {
      sb.append("and terminal_id='").append(mac).append("'");
    }
    sb.append(" GROUP BY a.id");
    objList = find(sb.toString());
    return objList;
  }

  /**
   * 
   * @author: huanghj
   * @Description: 查看文件夹类型的数据源执行计划
   * @param @param mac
   * @param @param userId
   * @param @param status
   * @param @return
   * @return List
   * @date 2015年11月11日上午11:54:10
   *
   * @修改人
   * @修改日期
   */
  public List findPlanListByUserId(String mac, String userId, String useSel) {
    List objList = null;
    StringBuffer sb=new StringBuffer();
    sb.append(" select a.id,name,next_run,status,ds_status,backup_cycle,backup_period ");
    sb.append(" from tb_datasource a , backup_strategy  b ");
    sb.append(" where  a.id=b.datasource_id and ds_type=0 and create_user_id= "+userId);
    if (mac != null && mac.length() != 0) {
      sb.append(" and terminal_id='" + mac + "'");
    }
    if(useSel!=null&&useSel.equals("1"))
    {
      sb.append(" and ds_status=0");
    }
    objList = find(sb.toString());
    return objList;
  }

  /**
   * 
   * @author: huanghj
   * @Description: TODO
   * @param @param mac
   * @param @param 查找启用状态的文件夹类型数据源
   * @param @return
   * @return List
   * @date 2015年11月11日下午1:01:36
   *
   * @修改人
   * @修改日期
   */
  public List findPlanUsedListByUserId(String mac, String userId) {
    List objList = null;
    String sql =
        "select id,name,bind_device_name,next_run,status,space_usage,ds_type from tb_datasource where ds_type=0 and status!=4 and create_user_id= "
            + userId;
    if (mac != null && mac.length() != 0) {
      sql += " and terminal_id='" + mac + "'";
    }
    objList = find(sql);
    return objList;
  }

  public Map findSafeBoxByUserId(String mac, String userId) {
    List objList = null;
    String sql = "select  (CASE WHEN SUM(space_usage) IS NULL THEN 0 ELSE SUM(space_usage) END)  as space_usage from tb_datasource where create_user_id="
        + userId + " and ds_type=2";
    if(mac!=null)
    {
      sql+=" and terminal_id='" + mac + "';";
    }
    
    objList = find(sql);
    if (objList != null && objList.size() > 0) {
      return (Map) objList.get(0);
    } else {
      return null;
    }
  }

  
  /**
   * 
   * @author fanyb
   * @Description: 批量变更数据源状态
   * @param ids
   * @param dsStatus
   * @return boolean 
   * @date Nov 12, 20154:22:24 PM
   *
   * @修改人 
   * @修改日期
   */
  public boolean updateDatasourcesStatus(String[] ids,int dsStatus){
    String values = ConvertUtil.toDbString(ids);
    String hql="update from Datasource d set d.dsStatus=:dsStatus where id in "+values;
    try {
      Map<String, Object> paraMap = new HashMap<String, Object>();
      paraMap.put("dsStatus", dsStatus);
      boolean deleteFlag = this.execHQL(hql, paraMap);
      return deleteFlag;
    } catch (Exception e) {
      e.printStackTrace();
    }
    return false;
  }
  
  /**
   * 
   * @author fanyb
   * @Description:根据ids查找运行状态列表
   * @param ids
   * @param status
   * @return List<Datasource> 
   * @date Nov 26, 20152:09:49 PM
   *
   * @修改人 
   * @修改日期
   */
  public List<Datasource> findDatasources(String[] ids,int status){
    String values = ConvertUtil.toDbString(ids);
    String hql="from Datasource d where d.status=:status and id in "+values;
    List<Datasource> datasources=null;
    try {
      Map<String, Object> paraMap = new HashMap<String, Object>();
      paraMap.put("status", status);
      datasources = queryObject(hql,paraMap);
    } catch (Exception e) {
      e.printStackTrace();
    }
    return datasources;
  }
  
  /**
   * 
   * @author fanyb
   * @Description: 根据创建人id和状态查找数据源列表
   * @param createUserId
   * @param status
   * @return List<Datasource> 
   * @date Nov 27, 20152:24:40 PM
   *
   * @修改人 
   * @修改日期
   */
  public List<Datasource> findByCreateUserIdAndDsType(String createUserId,String dsType){
    String hql="from Datasource d where d.dsType=:dsType and createUserId=:createUserId";
    List<Datasource> datasources=null;
    try {
      Map<String, Object> paraMap = new HashMap<String, Object>();
      paraMap.put("dsType", dsType);
      paraMap.put("createUserId", createUserId);
      datasources = queryObject(hql,paraMap);
    } catch (Exception e) {
      e.printStackTrace();
    }
    return datasources;
  }
}
